<template>
  <BasicDrawer
    @register="registerDrawer"
    :showFooter="showFooter"
    width="900px"
    :title="getTitle"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" class="menuForm" v-show="!isShowType">
      <template #reqMethod="{}">
        <span>POST</span>
      </template>
      <template #add="{ model, field }">
        <div class="addCol">
          <a-button
            shape="circle"
            type="primary"
            preIcon="ant-design:plus-outlined"
            @click="openPopup(model.source)"
            class="addBtn"
            :disabled="!showFooter"
            >&nbsp;</a-button
          >
          {{ model.source == 'WEBHOOK' ? '添加请求头' : '添加字段属性' }}
        </div>
      </template>
      <template #del="{ model, field }">
        <div class="addColInput">
          <a-input v-model:value="model[field]" />
          <a-button @click="del(field)" class="delBtn">-</a-button>
        </div>
      </template>
    </BasicForm>
  </BasicDrawer>
  <BasicModal
    @register="registerModal"
    title="数据源配置项"
    @ok="handleOk"
    @cancel="handleDataCancel"
    :height="270"
    :bodyStyle="{ marginLeft: '100px' }"
    :canFullscreen="false"
  >
    <BasicForm @register="registerForm1" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { formSchema, formSchema1, formSchema2 } from './dataSource.data';
  // import { saveOrUpdate } from './dataSource/api';
  const isUpdate = ref(true);
  const addType = ref('');
  const emit = defineEmits(['success', 'register']);
  const [registerModal, { openModal, closeModal }] = useModal();
  const [registerForm, { removeSchemaByFiled, appendSchemaByField, resetFields, setFieldsValue, validate, setProps }] = useForm({
    labelWidth: 200,
    schemas: formSchema,
    showActionButtonGroup: false,
  });
  const [registerForm1, { validate: validate1, resetFields: resetFields1 }] = useForm({
    schemas: addType.value == 'WEBHOOK' ? formSchema2 : formSchema1,
    //隐藏操作按钮
    showActionButtonGroup: false,
  });
  const showFooter = ref(true);
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    resetFields();
    showFooter.value = data?.showFooter ?? true;
    isUpdate.value = !!data?.isUpdate;
    setDrawerProps({ confirmLoading: false, showFooter: showFooter.value });
    if (unref(isUpdate)) {
      setFieldsValue({
        ...data.record,
      });
    }
    setProps({ disabled: !data?.showFooter });
  });
  //获取弹窗标题
  const getTitle = computed(() => (!unref(isUpdate) ? '新增数据源' : '编辑数据源'));
  // const authType = reactive(data.authType);
  //提交事件
  async function handleSubmit() {
    try {
      const values = await validate();
      console.log(values);
      setDrawerProps({ confirmLoading: true });
      //提交表单
      // await saveOrUpdate(values, isUpdate.value);
      handleDataCancel();
      closeDrawer();
      emit('success');
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
  async function openPopup(type) {
    addType.value = type;
    openModal(true, {});
  }
  const n = ref(1);
  async function handleOk() {
    const values = await validate1();
    appendSchemaByField(
      {
        field: values.params,
        label: values.params,
        component: 'Input',
        colProps: {
          span: 20,
        },
        slot: 'del',
      },
      ''
    );
    n.value++;
    handleDataCancel();
  }

  function handleDataCancel() {
    resetFields1();
    closeModal();
  }

  async function del(field) {
    removeSchemaByFiled([`${field}`]);
    n.value--;
  }
</script>
<style scoped>
  .addCol {
    margin-left: 200px;
  }
  .addBtn {
    margin-right: 20px;
  }
  .delBtn {
    margin-left: 20px;
  }
  .addColInput {
    display: flex;
  }
</style>
